package com.gax.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gax.pojo.User;
import com.gax.service.UserService;
import com.gax.mapper.UserMapper;
import com.gax.utils.JwtHelper;
import com.gax.utils.MD5Util;
import com.gax.utils.Result;
import com.gax.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;

/**
* @author 19115
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2025-06-16 19:50:31
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result login(User user) {
        // 根据账号，查询数据库，返回用户对象
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername,user.getUsername());
        User loginUser = userMapper.selectOne(lambdaQueryWrapper);
        // 如果用户对象为 null，则查询失败，返回账号错误，501
        if (loginUser == null) {
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        // 对比密码，失败，返回 503 错误
        if (StringUtils.hasText(user.getUserPwd())
                && MD5Util.encrypt(user.getUserPwd()).equals(loginUser.getUserPwd())) {
            // 登录成功，根据用户 id，生成一个 token，返回 token
            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUid()));
            // 将 token 封装到 result 返回
            HashMap map = new HashMap<>();
            map.put("token", token);
            return Result.ok(map);
        }
        // 密码错误
        return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
    }

    // 根据 token 获取用户数据
    @Override
    public Result getUserInfo(String token) {
        // 判断 token 是否在有效期
        // 如果为 true 则过期了
        boolean expiration = jwtHelper.isExpiration(token);
        if (expiration) {
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
        int userId = jwtHelper.getUserId(token).intValue();
        User user = userMapper.selectById(userId);
        user.setUserPwd("");
        HashMap data = new HashMap<>();
        data.put("loginUser",user);
        return Result.ok(data);
    }

    // 检查注册时，账号是否可用
    // 根据账号进行 count 查询
    // count == 0 可用
    // count > 0 不可用
    @Override
    public Result checkUserName(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        Long count = userMapper.selectCount(queryWrapper);
        if (count == 0) {
            return Result.ok(null);
        }
        return Result.build(null,ResultCodeEnum.USERNAME_USED);
    }

    // 注册业务
    @Override
    public Result regist(User user) {
        // 检查账号是否被占用
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        Long count = userMapper.selectCount(queryWrapper);
        if (count > 0) {
            return Result.build(null,ResultCodeEnum.USERNAME_USED);
        }
        // 用户名可用，加密密码
        user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
        userMapper.insert(user);
        return Result.ok(null);
    }
}




